import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:good_good_study/common/index.dart';
import 'package:good_good_study/common/widget/reminder/top_reminder.dart';
import 'package:good_good_study/pages/register_page.dart';
import 'package:good_good_study/provider/app_provider.dart';
import 'package:good_good_study/repository/http.dart';
import 'package:provider/provider.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  String username = '';

  /// 登录密码
  String pwd = '';

  bool _inLoading = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ColorUtil.color('#10234B'),
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: ColorUtil.color('#10234B'),
          elevation: 0,
          title: Text('登录',
              style: TextStyle(fontSize: 36.asSp, color: Colors.white)),
          leading: BackButton(),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
                child: Container(
                    child: SingleChildScrollView(
              physics: BouncingScrollPhysics(),
              child: Container(
                child: Column(
                  children: <Widget>[
                    288.vGap,
                    Container(
                      decoration: BoxDecoration(
                          border: Border(
                        bottom: BorderSide(
                            color: ColorUtil.color('#CCCCCC'), width: 0.4),
                      )),
                      margin: EdgeInsets.only(
                          left: 74.asWidth,
                          right: 74.asWidth,
                          bottom: 48.asHeight),
                      height: 80.asHeight,
                      child: Row(
                        children: [
                          Icon(
                            Icons.supervised_user_circle,
                            color: Colors.white,
                          ),
                          15.hGap,
                          Expanded(
                            child: TextField(
                              keyboardType: TextInputType.text,
                              style: TextStyle(
                                  fontSize: 32.asSp,
                                  color: Colors.white,
                                  fontWeight: FontWeight.w600),
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintStyle: TextStyle(
                                    fontSize: 32.asSp,
                                    color: ColorUtil.color('#999999')),
                                hintText: '请输入用户名',
                              ),
                              onChanged: (str) {
                                username = str;
                              },
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      height: 80.asHeight,
                      width: ScreenAdapter.screenWidthDp,
                      decoration: BoxDecoration(
                          border: Border(
                        bottom: BorderSide(
                            color: ColorUtil.color('#CCCCCC'), width: 0.4),
                      )),
                      margin:
                          EdgeInsets.only(left: 74.asWidth, right: 74.asWidth),
                      child: Row(
                        children: [
                          Icon(
                            Icons.link,
                            color: Colors.white,
                          ),
                          15.hGap,
                          Expanded(
                            child: TextField(
                              keyboardType: TextInputType.text,
                              obscureText: true,
                              style: TextStyle(
                                  fontSize: 32.asSp,
                                  color: Colors.white,
                                  fontWeight: FontWeight.w600),
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintStyle: TextStyle(
                                    fontSize: 32.asSp,
                                    color: ColorUtil.color('#999999')),
                                hintText: '请输入密码',
                              ),
                              onChanged: (str) {
                                pwd = str;
                              },
                            ),
                          ),
                          Container(
                            color: ColorUtil.color('#747C8F'),
                            height: 34.asHeight,
                            width: 2.asWidth,
                          ),
                        ],
                      ),
                    ),
                    80.vGap,
                    Container(
                      margin: EdgeInsets.symmetric(horizontal: 74.asWidth),
                      decoration: BoxDecoration(
                        gradient: LinearGradient(colors: [
                          ColorUtil.color('#0D86F8'),
                          ColorUtil.color('#66EEAC'),
                        ]),
                        borderRadius: BorderRadius.circular(6.asWidth),
                      ),
                      child: FLLoadingButton(
                        child: Text(
                          '登录',
                          style: TextStyle(fontSize: 36.asSp),
                        ),
                        disabledColor: Theme.of(context).primaryColorLight,
                        disabledTextColor: Colors.white.withAlpha(160),
                        indicatorColor: Colors.white,
                        textColor: Colors.white,
                        loading: _inLoading,
                        elevation: 5,
                        minWidth: ScreenAdapter.screenWidthDp,
                        height: 45,
                        onPressed: () {
                          doLogin(context);
                        },
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(
                        top: 69.asHeight,
                      ),
                      child: Text.rich(TextSpan(children: [
                        TextSpan(
                            text: "新用户",
                            style: TextStyle(
                              color: ColorUtil.color('#999999'),
                              fontSize: 28.asSp,
                            )),
                        TextSpan(
                            text: '注册',
                            recognizer: new TapGestureRecognizer()
                              ..onTap = () => {
                                    RouteUtil.push(context, RegisterPage())
                                        .then((value) {
                                      if (value == true) {
                                        Navigator.pop(context);
                                      }
                                    }),
                                  },
                            style: TextStyle(
                              color: ColorUtil.color('#66EEAC'),
                              fontSize: 28.asSp,
                            )),
                      ])),
                    ),
                    20.vGap,
                  ],
                ),
              ),
            ))),
            68.vGap
          ],
        ));
  }

  void doLogin(BuildContext context) {
    setState(() {
      _inLoading = true;
    });
    Http.userLogin(username, pwd).listen((loginEntity) {
      context
          .read<AppProvider>()
          .setUserLoginData(loginEntity)
          .then((value) => Navigator.pop(context));
    }, onError: (err) {
      if (err is NetError) {
        TopReminder.error(context, err.msg);
      }
    }, onDone: () {
      setState(() {
        _inLoading = false;
      });
    });
  }
}
